/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddDiselToMachine_Copy.java
 *
 * Created on Oct 2, 2014, 4:03:29 PM
 */
package com.JD.DiselLog;

import com.JD.Disel.Hibernate.Rawdiselusedlog;
import com.JD.datePicker.DatePicker;
import com.JD.master.Hibernate.PartyMaster;
import com.JD.master.Validation.Validator;
import java.util.*;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author admin
 */
public class AddDiselToMachine_Copy extends javax.swing.JFrame {

    /** Creates new form AddDiselToMachine_Copy */
    com.JD.master.Validation.Validator validator = new Validator();
    String usedBy = "";
    String usedQuantity = "";
    String totalQuantityAvilable = "";
    String dateOfAddition = "";
    String timeOfAddition = "";
    String addedBy = "";
    String rawField1 = "NO";
    String rawField2 = "NO";
    String rawField3 = "NO";
    String rawField4 = "NO";
    public AddDiselToMachine_Copy() {
        initComponents();


        SessionFactory sessionFactory = com.JD.master.Hibernate.Master_HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        //  com.JD.master.Hibernate.MachineMaster

        Query query = session.createQuery("from com.JD.master.Hibernate.MachineMaster");

        List list = query.list();


        for (Object object : list) {

            com.JD.master.Hibernate.MachineMaster machineMaster = (com.JD.master.Hibernate.MachineMaster) object;

            machineList_Combobox.addItem(machineMaster.getMachineName());
        }




    }

   

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        AddDiselToMachine_Panel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        partyTable = new javax.swing.JTable();
        machineList_Combobox = new javax.swing.JComboBox();
        quantityProvide_TextField = new javax.swing.JTextField();
        addedBy_TextField = new javax.swing.JTextField();
        addDataToDatabase_Button = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        AddDiselToMachine_Panel.setBackground(new java.awt.Color(255, 102, 51));
        AddDiselToMachine_Panel.setPreferredSize(new java.awt.Dimension(1135, 627));

        partyTable.setFont(new java.awt.Font("Traditional Arabic", 0, 14));
        partyTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Machine Name", "Quantity Added", "Added By", "Total Quantity", "Added On"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(partyTable);

        quantityProvide_TextField.setFont(new java.awt.Font("Traditional Arabic", 0, 14));
        quantityProvide_TextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                quantityProvide_TextFieldKeyReleased(evt);
            }
        });

        addedBy_TextField.setFont(new java.awt.Font("Traditional Arabic", 0, 14));

        addDataToDatabase_Button.setFont(new java.awt.Font("Traditional Arabic", 0, 14));
        addDataToDatabase_Button.setText("Add");
        addDataToDatabase_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addDataToDatabase_ButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Traditional Arabic", 1, 14));
        jLabel1.setText("Quantity:");

        jLabel2.setFont(new java.awt.Font("Traditional Arabic", 1, 14));
        jLabel2.setText("Added By:");

        javax.swing.GroupLayout AddDiselToMachine_PanelLayout = new javax.swing.GroupLayout(AddDiselToMachine_Panel);
        AddDiselToMachine_Panel.setLayout(AddDiselToMachine_PanelLayout);
        AddDiselToMachine_PanelLayout.setHorizontalGroup(
            AddDiselToMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AddDiselToMachine_PanelLayout.createSequentialGroup()
                .addGroup(AddDiselToMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1050, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(AddDiselToMachine_PanelLayout.createSequentialGroup()
                        .addGap(313, 313, 313)
                        .addGroup(AddDiselToMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(83, 83, 83)
                        .addGroup(AddDiselToMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(quantityProvide_TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
                            .addComponent(addedBy_TextField, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(addDataToDatabase_Button)
                            .addComponent(machineList_Combobox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(125, Short.MAX_VALUE))
        );
        AddDiselToMachine_PanelLayout.setVerticalGroup(
            AddDiselToMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, AddDiselToMachine_PanelLayout.createSequentialGroup()
                .addGap(134, 134, 134)
                .addComponent(machineList_Combobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43)
                .addGroup(AddDiselToMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(quantityProvide_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(33, 33, 33)
                .addGroup(AddDiselToMachine_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addedBy_TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(27, 27, 27)
                .addComponent(addDataToDatabase_Button)
                .addGap(175, 175, 175)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(AddDiselToMachine_Panel, javax.swing.GroupLayout.DEFAULT_SIZE, 1175, Short.MAX_VALUE)
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(AddDiselToMachine_Panel, javax.swing.GroupLayout.PREFERRED_SIZE, 718, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void quantityProvide_TextFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_quantityProvide_TextFieldKeyReleased
        // TODO add your handling code here:

         quantityProvide_TextField.setText(validator.numberValidator(quantityProvide_TextField.getText()));
    }//GEN-LAST:event_quantityProvide_TextFieldKeyReleased

      AddDataToDataBase addDataToDataBase = new AddDataToDataBase();
    int currentQuantity = Integer.parseInt(addDataToDataBase.getCurrentQuentity());

    private void addDataToDatabase_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addDataToDatabase_ButtonActionPerformed
        // TODO add your handling code here:


        usedQuantity = quantityProvide_TextField.getText();

        int chk = Integer.parseInt(usedQuantity);

        if (chk <= currentQuantity) {

            totalQuantityAvilable = Integer.parseInt(addDataToDataBase.getCurrentQuentity()) - Integer.parseInt(quantityProvide_TextField.getText()) + "";
            usedBy = machineList_Combobox.getSelectedItem().toString();

            addedBy = addedBy_TextField.getText();

             SessionFactory sessionFactory=com.JD.Disel.Hibernate.DiselLog_HibernateUtil.getSessionFactory();
             Session session=sessionFactory.openSession();
             Transaction transaction=session.beginTransaction();

             com.JD.Disel.Hibernate.Rawdiselusedlog rawdiselusedlog=new Rawdiselusedlog(usedBy, usedQuantity, totalQuantityAvilable, dateOfAddition, timeOfAddition, addedBy, rawField1, rawField2, rawField3, rawField4);
             session.save(rawdiselusedlog);
             transaction.commit();


            AddDataToDataBase addDataToDataBase=new AddDataToDataBase(totalQuantityAvilable);

            new UpdateDiselQuantityOfMachine(usedBy, usedQuantity);

           
            quantityProvide_TextField.setText("");
            addedBy_TextField.setText("");




        }

        else{
            JOptionPane.showMessageDialog(null,"Demand Can Not Be Full Fill");
        }


    }//GEN-LAST:event_addDataToDatabase_ButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JPanel AddDiselToMachine_Panel;
    private javax.swing.JButton addDataToDatabase_Button;
    private javax.swing.JTextField addedBy_TextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox machineList_Combobox;
    private javax.swing.JTable partyTable;
    private javax.swing.JTextField quantityProvide_TextField;
    // End of variables declaration//GEN-END:variables

    public JPanel getAddDiselToMachine_Panel() {
        return AddDiselToMachine_Panel;
    }
}
